package com.app.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.app.entity.Customer;
import com.app.service.CustomerService;
import com.github.pagehelper.PageInfo;

@Controller
public class Go {
	@Autowired
	CustomerService customerService;

	@RequestMapping("/reg.shtml")
	public String lunch(@RequestParam(value = "arg", required = false) String arg, HttpServletRequest request,
			HttpSession session) {
		// HKEY_CURRENT_USER\SOFTWARE\Tencent\LOL InstallPath QQlogin
		try {
			Process ps = null;
			ps = Runtime.getRuntime().exec("reg query HKEY_CURRENT_USER\\SOFTWARE\\Tencent\\LOL");
			ps.getOutputStream().close();
			InputStreamReader i = new InputStreamReader(ps.getInputStream());
			BufferedReader ir = new BufferedReader(i);

			// int aa, ii = 0;
			// char[] b = new char[1024];
			// for (aa = ir.read(); aa != -1; aa = ir.read())
			// b[ii++] = (char) aa;
			// String s = new String(b, 0, ii);

			StringBuffer s = new StringBuffer();
			for (String aa = ir.readLine(); aa != null; aa = ir.readLine())
				s.append(aa);

			int endIndex = s.indexOf("Client.exe");
			int startIndex = s.lastIndexOf(":\\", endIndex);
			String path = s.substring(startIndex - 1, endIndex + 10);
			Runtime.getRuntime().exec("cmd /c start " + path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Logger logger = LoggerFactory.getLogger(Go.class);
		logger.info("slf4j与log4j日志系统");
		return "reg.vm";
	}

	@RequestMapping("/velocity.shtml")
	public String vmtest(Model model, HttpServletRequest request) {
		model.addAttribute("hello", "hello,velocity");
		return "velocity.vm";
	}

	@RequestMapping("/show.shtml")
	public String show(Model model, HttpServletRequest request) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		String start = request.getParameter("start");
		queryMap.put("start", start == null ? 1 : start);
		String limit = request.getParameter("limit");
		queryMap.put("limit", limit == null ? 2 : limit);
		PageInfo<Customer> pageInfo = customerService.selectAllCustomer(queryMap);
		model.addAttribute("pageInfo", pageInfo);
		return "show.vm";
	}
}